package com;

import org.apache.log4j.PropertyConfigurator;

import com.rapplogic.xbee.transparent.XBeeSerialProgrammer;


public class XBeeConfigure {
	
	private String COM = "COM7";
	
	XBeeSerialProgrammer prog = new XBeeSerialProgrammer();
	
	public void setRobotDestAdress() throws Exception{
		try {
			prog.open(COM, "XBeeApi", 0, 9600);
			prog.addCommand("ATDL 4321");
			prog.addCommand("ATWR");
			prog.execute();	
		} finally {
			prog.close();
		}
	}
	
	public void setNodeDestAdress() throws Exception {
		try {
			prog.open(COM, "XBeeApi", 0, 9600);
			prog.addCommand("ATDL FFFF");
			prog.addCommand("ATWR");
			prog.execute();	
		} finally {
			prog.close();
		}
	}
	
	public void setDestAdress(String adress) throws Exception {
		try {
			prog.open(COM, "XBeeApi", 0, 9600);
			prog.addCommand("ATDL " + adress);
			prog.addCommand("ATWR");
			prog.execute();	
		} finally {
			prog.close();
		}
	}
	
	public void setPanId(String id) throws Exception {
		try {
			prog.open(COM, "XBeeApi", 0, 9600);
			prog.addCommand("ATID " + id);
			prog.addCommand("ATWR");
			prog.execute();	
		} finally {
			prog.close();
		}
	}
	
	public String getPanId() throws Exception {
		// not working
		String response = null;
		try {
			prog.open(COM, "XBeeApi", 0, 9600);
			prog.addCommand("ATID ");
			prog.execute();
		} finally {
			response = prog.getLastResponse();
			prog.close();
		}
		return response;
	}
	
	public static void main(String[] args) throws Exception {
		PropertyConfigurator.configure("log4j.properties");
		//for test
		//XBeeConfigure test = new XBeeConfigure();
		//System.out.print("PAN ID is " + test.getPanId());
		//test.setNodeDestAdress();
	}
}
